define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/package/index' + location.search,
                    add_url: 'wanlshop/package/add',
                    edit_url: 'wanlshop/package/edit',
                    del_url: 'wanlshop/package/del',
                    multi_url: 'wanlshop/package/multi',
                    import_url: 'wanlshop/package/import',
                    table: 'wanlshop_package',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'wanlshoporder.order_no', title: __('Wanlshoporder.order_no'), operate: 'LIKE'},
                        {field: 'package_no', title: __('Package_no'), operate: 'LIKE'},
                        {field: 'delivery_mode', title: __('Delivery_mode'), searchList: {"created":__('Delivery_mode created'),"self":__('Delivery_mode self'),"instead":__('Delivery_mode instead')}, formatter: Table.api.formatter.normal},
                        {field: 'user.mobile', title: __('会员手机号'), operate: 'LIKE'},
                        {field: 'delivery_status', title: __('Delivery_status'), searchList: {"0":__('Delivery_status 0'),"1":__('Delivery_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'express_name', title: __('Express_name'), operate: 'LIKE'},
                        {field: 'express_no', title: __('Express_no'), operate: 'LIKE'},
                        {field: 'idcard_status', title: __('Idcard_status'), searchList: {"0":__('Idcard_status 0'),"1":__('Idcard_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'delivertime', title: __('Delivertime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,buttons: [
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'wanlshop/package/detail',
                                    extend:"data-area='[\"80%\",\"80%\"]'",
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },

                            ], events: Table.api.events.operate, formatter: Table.api.formatter.buttons}
                    ]
                ]
            });
            // 发货 & 批量发货
            $(document).on("click", ".btn-delivery", function () {
                if($(this).data('id')){
                    Backend.api.open('wanlshop/package/delivery/ids/' + $(this).data('id'), __('发货'),{area:['80%', '80%']});
                }else{
                    Backend.api.open('wanlshop/package/delivery/ids/' + Table.api.selectedids(table), __('批量发货'),{area:['80%', '80%']});
                }
            });

            // 打印 & 批量打印订单 自动关闭窗口parent.Layer.closeAll();
            $(document).on("click", ".btn-invoice", function () {
                if($(this).data('id')){
                    Backend.api.open('wanlshop/package/invoice/ids/' + $(this).data('id'), __('查看发货单'),{area:['80%', '80%']});
                }else{
                    Backend.api.open('wanlshop/package/invoice/ids/' + Table.api.selectedids(table), __('批量查看发货单'),{area:['80%', '80%']});
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'wanlshop/package/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'wanlshop/package/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'wanlshop/package/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail:function(){
            Controller.api.bindevent();
            // 发货 & 批量发货
            $(document).on("click", ".btn-delivery", function () {
                if($(this).data('id')){
                    Backend.api.open('wanlshop/package/delivery/ids/' + $(this).data('id'), __('发货'),{area:['80%', '80%']});
                }else{
                    Backend.api.open('wanlshop/package/delivery/ids/' + Table.api.selectedids(table), __('批量发货'),{area:['80%', '80%']});
                }
            });
            //物流追踪
            $(document).on("click", ".btn-track", function () {
                Backend.api.open('wanlshop/package/relative/ids/' + $(this).data('id'), __('物流追踪'),{area:['50%', '80%']});
            });
            //打单
            $(document).on("click", ".btn-print", function () {
                Backend.api.open('wanlshop/package/invoice/ids/' + $(this).data('id'), __('打单'),{area:['50%', '100%']});
            });

            $(document).on("click", ".btn-upload-idcard", function () {
                var package_id = $(this).data('id');
                var confirm = Layer.confirm('确认上传客户身份证信息?',
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        var loadding = Layer.load();
                        Fast.api.ajax({
                            'url' : 'wanlshop/package/uploadIdcard',
                            'data':{
                                'package_id':package_id
                            }
                        },function () {
                            Layer.close(confirm);
                            Layer.close(loadding);
                        },function () {
                            Layer.close(confirm);
                            Layer.close(loadding);
                        })
                    }
                );
            });


            $(document).on("click", ".btn-upload", function () {
                var ids = $(this).data('id');
                var top = $(this).offset().top;
                var that = this;
                var id = "upload-image";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "上传包裹图片",
                    area: ["600px", "250px"],
                    offset: top - 50,
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        var newUrl = 'wanlshop/package/edit/ids/' + ids;
                        $( "#upload-form" ).attr( 'action' ,newUrl);
                        Form.api.bindevent(form, function (data) {
                            location.reload();
                            layer.close(layero);
                        });
                    }
                });
            });

            $(document).on("click",".btn-package-image",function () {
                var data = [];
                var value = $(this).data('value') === null ? '' : $(this).data('value').toString();
                var arr = value != '' ? value.split(",") : [];
                $.each(arr, function (index, value) {
                    data.push({
                        src: Fast.api.cdnurl(value),
                    });
                });
                Layer.photos({
                    photos: {
                        "start": 0,
                        "data": data
                    },
                    anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });
            })
        },
        delivery:function(){
            Controller.api.bindevent();
        },
        relative:function(){

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});